package com.iraeta.controller.basedata;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.GongYiType;
import com.iraeta.entity.Gongyi;
import com.iraeta.service.GongYiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController(value = "GongYiController")
@RequestMapping("gongyi")
public class GongYiController extends BaseController {
    @Autowired
    GongYiService gongYiService;

    @RequestMapping("pagegongyi")
    public String pageGongYi(@RequestBody JSONObject jo){
        String gongyitype = jo.getString("gongyitype");
        String cname = jo.getString("cname");
        this.pageSet(jo);
        List<Gongyi> gongyiList = gongYiService.listGongyi(gongyitype, cname);
        PageInfo<Gongyi> gongyiPageInfo = this.getPageInfo(gongyiList);
        return this.success(gongyiPageInfo);
    }

    @RequestMapping("getgongyi")
    public String getGongYi(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Gongyi gongyi = gongYiService.getGongyi(id);
        return this.success(gongyi);
    }

    @RequestMapping("addgongyi")
    public String addEditGongYi(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Gongyi gongyi = id == 0 ? new Gongyi() :  gongYiService.getGongyi(id);
        this.mapJsonToObj(paramJb, gongyi);
        if(id == 0){
            gongyi.setDeleted(0);
            gongYiService.addGongyi(gongyi);
        }else {
            gongYiService.updateGongyi(gongyi);
        }
        return this.success();
    }

    @RequestMapping("deletegongyi")
    public String deleteGongYi(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        gongYiService.deleteGongyi(id);
        return this.success();
    }

    /*获取所有工艺*/
    @RequestMapping("listallgongyi")
    public String listAllGongYi(){
        List<Gongyi> gongyiList = gongYiService.listAllGongyi();
        return this.success(gongyiList);
    }

    @RequestMapping("listjiaregongyi")
    public String listJiaReGongYi(){
        List<Gongyi> gongyiList = gongYiService.listGongyi(GongYiType.jiare, null);
        return this.success(gongyiList);
    }

    /*材质和工艺关联内容*/
    @RequestMapping("listcaizhigongyi")
    public String listCaiZhiGongYi(@RequestBody JSONObject paramJb){
        int caizhiid = paramJb.getIntValue("id");
        String gongyitype = paramJb.getString("gongyitype");
        List<Gongyi> gongyiList = gongYiService.listCaiZhiGongyi(gongyitype, caizhiid);
        //List<Integer> gongYiIdList = gongyiList.stream().map(Gongyi::getId).collect(Collectors.toList());
        return this.success(gongyiList);
    }

    @RequestMapping("savecaizhigongyi")
    public String saveCaiZhiGongYi(@RequestBody JSONObject paramJb){
        int caizhiid = paramJb.getIntValue("caizhiid");
        String gongyitype = paramJb.getString("gongyitype");
        JSONArray gongYiJa = paramJb.getJSONArray("gongyichecked");
        try {
            gongYiService.saveCaiZhiGongyi(caizhiid, gongyitype, gongYiJa);
            return this.success();
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }
}
